import React from 'react';
import { observer } from 'mobx-react';
import {toJS} from "mobx";
import { Upload } from 'weaver-mobile-page';
import {_, FieldUtil} from "../../../util/pcPublic";
import * as emSDK from "../../../util/emSDK";
import { Icon } from 'weaver-mobile';

@observer
export default class AddIcon extends React.Component{

    constructor(props) {
        super(props);
    }

    doChangeEvent(ids,filedatas){
        const { fieldid, wfLayoutStore, listStore } = this.props;
        const { mainData } = wfLayoutStore;
        const fieldMark = `field${fieldid}`;
        let fieldObj = FieldUtil.getMainFieldObj(fieldMark, mainData);
        //当前字段的只读、必填属性
        let viewAttr = FieldUtil.getFieldCurViewAttr(fieldMark, fieldObj);
        const {fieldStore} = fieldObj;
        let specialobj  = fieldObj.specialobj ? toJS(fieldObj.specialobj) : {};
        filedatas.map(item=>{  delete item.buttonGroup;})
        specialobj.filedatas = filedatas;
        wfLayoutStore.changeSingleFieldValue(fieldMark, {value:ids.join(","), specialobj:specialobj});

        listStore.setState({ data: filedatas });
    }


    render() {
        const { fieldid, wfLayoutStore, wfGlobalStore, selectedCatalog } = this.props;
        const { mainData, variableMap, fieldAttrMap } = wfLayoutStore;
        const fieldMark = `field${fieldid}`;
        let fieldObj = FieldUtil.getMainFieldObj(fieldMark, mainData);
        const { value,variable } = fieldObj;
        //计算字段类型(支持动态切换字段类型)
        const fieldAttr = fieldAttrMap.get(fieldid);
        let htmltype = fieldAttr.htmltype || 0;
        let detailtype = fieldAttr.detailtype || 0;
        if(variable.has("changeFieldType") && variable.get("changeFieldType").indexOf("-")>0){
            const typeArr = variable.get("changeFieldType").split("-");
            htmltype = parseInt(typeArr[0]);
            detailtype = parseInt(typeArr[1]);
        }
        //当前字段的只读、必填属性
        let viewAttr = FieldUtil.getFieldCurViewAttr(fieldMark, fieldObj);
        //字段值,特殊字符转换
        const fieldValue = FieldUtil.restoreSpeicalChar(fieldid, value.toString(), htmltype, detailtype, viewAttr);


        const { fileattr } = fieldAttr;
        let specialobj  = fieldObj.specialobj ? toJS(fieldObj.specialobj) : {};
        const { filedatas } = specialobj;
        //计算附件上传目录  catelogType = 0:固定目录 1：选择目录
        let category = "";
        let { catelogType,docCategory,selectedCateLog,maxUploadSize,limitType }  = fileattr;
        if(catelogType === 0){
            if(_.trim(docCategory).length > 0){
                category = _.trim(docCategory);
            }
        }else{
            if(!_.isEmpty(selectedCatalog)){
                if(!selectedCatalog.errorMsg){
                    category = selectedCatalog && selectedCatalog.docCategory;
                    maxUploadSize = selectedCatalog && selectedCatalog.maxUploadSize;
                }
            }
        }
        const { commonParam } = wfGlobalStore;
        const { workflowid,requestid,authStr,authSignatureStr,f_weaver_belongto_userid,f_weaver_belongto_usertype,isviewonly } = commonParam;

        const listType = detailtype === 2 ? "img" : "list";
        const uploadUrl = "/api/doc/upload/uploadFile2Doc";
        const uploadParams = {
            category,workflowid,listType,authStr,authSignatureStr,f_weaver_belongto_userid,f_weaver_belongto_usertype
        };
        let fileProps = { uploadUrl,uploadParams,category,listType,limitType,maxUploadSize };
        fileProps.uploadId = fieldMark;
        fileProps.imgColNumber = 1;
        fileProps["editable"] = false;
        fileProps["showFileList"] = false;
        fileProps["viewAttr"] = viewAttr == 3 ? 2 : viewAttr ;

        filedatas && filedatas.map(item => {
            if (item.fileExtendName != "pdf") {
                emSDK.changeFileid(item);
            }
        })
        fileProps.datas = filedatas;

        fileProps.children = (
            <Icon type="plus" size="xs" color="white"
            style={{background:'#55b1f9', width: '18px', height: '18px', padding: '5px'}} />);

        return (
            <Upload {...fileProps} onChange={this.doChangeEvent.bind(this)} />
        )
    }

}